package com.example.demowebsocket;

import com.example.demowebsocket.redis.RedisPusUtils;
import com.example.demowebsocket.redis.User;
import com.example.demowebsocket.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * WebSocketController
 *
 * @author zhengkai.blog.csdn.net
 */
@RestController
public class DemoController {
    @Autowired
    private RedisPusUtils redisPusUtils;

    @GetMapping("test")
    public ResponseEntity<String> test() {
        System.out.println("请求成功");
        return ResponseEntity.ok("请求成功");
    }

    @GetMapping("push")
    public ResponseEntity<String> pushToWeb(String toUserId) throws IOException {
        WebSocketServer.sendInfo("hello world", toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }

    @GetMapping("send")
    public String send() {
        User user = new User(10, "尚***", 26, "男", "陕西省xxxx市xxxxxx县");
        // 通道名称，消息
        redisPusUtils.publish("phone", user);
        return "";
    }
}
